package com.lovo.psc.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.psc.bean.TendersBean;
import com.lovo.psc.service.IBidService;
import com.lovo.psc.service.ITenderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("tender")
public class TenderController {
    @Autowired
    ITenderService tenderService;
    @Autowired
    IBidService bidService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("findAll/{id}/{pageNo}")
    public Page<TendersBean> findAll(@PathVariable("id") int id, @PathVariable int pageNo){
        return tenderService.findAll(id,pageNo);
    }
    @RequestMapping("findByStatus/{status}")
    public List<TendersBean> findByStatus(@PathVariable("status") int status){
        return tenderService.findByStatus(status);
    }
    @RequestMapping("findById/{tendersId}")
    public TendersBean findById(@PathVariable("tendersId") int tendersId){
        return tenderService.findById(tendersId);
    }

    @RequestMapping("updateStatus")
    public String updateStatus(String vendorName,int id, int status, String src) {

        tenderService.updateStatus(id, status);
        bidService.updateSrc(vendorName,id, src);
        String tonderJson = JSON.toJSONString(src);
        rabbitTemplate.convertAndSend("directExchangeTenders", "replenishUserTenders.key", tonderJson);
        return "ok";
    }

}
